package www17nyy.com.itext5;

import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.Image;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.ColumnText;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfPageEventHelper;
import com.lowagie.text.pdf.PdfTemplate;
import com.lowagie.text.pdf.PdfWriter;

import java.io.File;
import java.io.InputStream;
import java.net.URI;
import java.net.URL;
import java.util.Base64;


public class PdfXofyBuilder extends PdfPageEventHelper {

    public PdfTemplate total;

    public BaseFont bf;


    public void onOpenDocument(PdfWriter writer, Document document) {
        total = writer.getDirectContent().createTemplate(50, 50);
        total.setRGBColorFill(128, 128, 128);
    }


    public void onCloseDocument(PdfWriter writer, Document document) {
        try {
            bf = BaseFont.createFont();//BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", false);

        } catch (Exception e) {
            e.printStackTrace();
        }
        total.beginText();
        total.setFontAndSize(bf, 12);
        String foot2 = "" + (writer.getPageNumber() - 1);
        total.showText(foot2);
        total.endText();
        total.closePath();
    }

    public static int getPercent3(float h, float w) {
        int p = 0;
        float p2 = 0.0f;
        p2 = 520 / h * 10;
        p = Math.round(p2);
        return p;
    }

    @Override
    public void onEndPage(PdfWriter writer, Document document) {

//        llx Left ，lly  Bottom,urx  Right，ury  Top
//          Width=Right - Left   Heigth = Top - Bototom

//        http://www.17nyy.com/usr/themes/7tec/img/17nyy.ico

        Font fontDetail = null;

        try {
            if (bf == null) {
                bf = BaseFont.createFont();//BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", false);
            }
            if (fontDetail == null) {
                fontDetail = new Font(bf, 12, Font.NORMAL);
                fontDetail.setColor(128, 128, 128);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        PdfContentByte cb = writer.getDirectContent();


        String foot1 = "page " + writer.getPageNumber() + " of ";

        Phrase footer = new Phrase(foot1, fontDetail);


        float addTop = 15F;


        Image img = null;
        try {
            URL url = new File("src\\main\\resources\\templates\\17nyy.png").toURI().toURL();
            img = Image.getInstance(url);
            img.setAbsolutePosition(document.left() + 495F, document.top() - 65F);
            float heigth1 = img.getHeight();
            float width1 = img.getWidth();
            int percent1 = getPercent3(heigth1, width1);
            img.scalePercent(percent1);
            cb.addImage(img);
        } catch (Exception e) {
            e.printStackTrace();
        }


        //page of
        ColumnText.showTextAligned(
                cb,
                Element.ALIGN_RIGHT,
                footer,
                document.getPageSize().getRight() - 60F,
                document.getPageSize().getTop() - addTop, 0);

        // toal
        cb.addTemplate(total, document.getPageSize().getRight() - 55F,
                document.getPageSize().getTop() - addTop);

    }
}
